package com.jiuyou.clientController.mall.bbs;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Member;
import com.ruoyi.common.core.domain.model.LoginMember;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.shopping.domain.Bbs;
import com.ruoyi.shopping.domain.BbsComments;
import com.ruoyi.shopping.service.IBbsCommentsService;
import com.ruoyi.shopping.service.IBbsService;
import com.ruoyi.wechat.util.CommonUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author chengliang
 * @date 2022/6/20
 **/
@RestController
@RequestMapping("/client/mall/bbs/comments")
public class MallBBSCommentsClientController extends ClientBaseController {
    @Autowired
    private IBbsCommentsService commentsService;
    @Autowired
    private IBbsService bbsService;
    @PostMapping("/add")
    public AjaxResult add(BbsComments comments){
        if(comments.getParentLevelId()>0){
            BbsComments bbsComments = commentsService.selectBbsCommentsById(comments.getParentLevelId());
            comments.setParentPublishName(bbsComments.getPublishName());
        }
        Member loginMember = getLoginMember().getMember();
        comments.setPublishId(loginMember.getId());
        comments.setPublishHead(loginMember.getHeadImg());
        comments.setPublishName(loginMember.getNickName());
//        comments.setParentLevelId(bbsComments.getId());
        commentsService.insertBbsComments(comments);
        BbsComments bbs = commentsService.selectBbsCommentsById(comments.getId());
        JSONObject sJson = new JSONObject();
        sJson.put("topId",bbs.getId());
        sJson.put("id",bbs.getId());
        sJson.put("publishHead",bbs.getPublishHead());
        sJson.put("publishName",bbs.getPublishName());
        sJson.put("mainBody",bbs.getMainBody());
        sJson.put("createTime", comTimeStr(bbs.getCreateTime()));
        sJson.put("parentName",bbs.getParentPublishName());
        sJson.put("secondFlag",bbs.getParentId()>0?true:false);
        sJson.put("replyComments",new ArrayList<Object>());
        return AjaxResult.success(sJson);
    }

    @PostMapping("/getCount/{bbsId}")
    public AjaxResult getCount(@PathVariable Long bbsId){
        Integer count = commentsService.selectCountByBBsId(bbsId);
        return AjaxResult.success(count);
    }

    @PostMapping("/getList")
    public AjaxResult getList(Long bbsId){
        startPage();
        List<BbsComments> list = commentsService.selectListByBBsId(bbsId);
        List<JSONObject> obList = new ArrayList<>();
        TableDataInfo dataTable = getDataTable(list);
        for (BbsComments bbsComments : list) {
            startPage(1,2);
            JSONObject jso = new JSONObject();
            ArrayList<JSONObject> secondJso = new ArrayList<>();
            List<BbsComments> secondList = commentsService.selectListByParentId(bbsComments.getId());
            boolean b = !getDataTable(secondList).isLastPage();
            jso.put("topId",bbsComments.getId());
            jso.put("createTime",comTimeStr(bbsComments.getCreateTime()));
            jso.put("publishName",bbsComments.getPublishName());
            jso.put("publishHead",bbsComments.getPublishHead());
            jso.put("mainBody",bbsComments.getMainBody());
            jso.put("secondFinished",b);
            secondList.forEach(e->{
                JSONObject sJson = new JSONObject();
                sJson.put("id",e.getId());
                sJson.put("publishHead",e.getPublishHead());
                sJson.put("publishName",e.getPublishName());
                sJson.put("mainBody",e.getMainBody());
                sJson.put("createTime",comTimeStr(e.getCreateTime()));
                sJson.put("parentName",e.getParentPublishName());
                secondJso.add(sJson);
            });
            jso.put("replyComments",secondJso);
//            jso.put("finished",dataTable.isLastPage());
            obList.add(jso);
        }
        dataTable.setRows(obList);
        return AjaxResult.success(dataTable);
    }

    @PostMapping("/loadMore")
    public AjaxResult loadMore(Long maxId){
        int limit = 5;
        BbsComments bbsComments = commentsService.selectBbsCommentsById(maxId);
        List<BbsComments> list = commentsService.loadMore(bbsComments.getId(),bbsComments.getBbsId(),bbsComments.getParentId(),limit);
        List<JSONObject> ob = new ArrayList<JSONObject>();
        boolean lastPage = getDataTable(list).isLastPage();
        list.forEach(e->{
            JSONObject sJson = new JSONObject();
            sJson.put("id",e.getId());
            sJson.put("publishHead",e.getPublishHead());
            sJson.put("publishName",e.getPublishName());
            sJson.put("mainBody",e.getMainBody());
            sJson.put("createTime",comTimeStr(e.getCreateTime()));
            sJson.put("parentName",e.getParentPublishName());
            sJson.put("finished",lastPage);
            ob.add(sJson);
        });
        return AjaxResult.success(ob);
    }
    private String comTimeStr(Date date){

        Date dateNow=new Date();
        String yearNow=DateUtils.parseDateToStr(DateUtils.YYYY,dateNow);
        String ymdNow=DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,dateNow);

        String yearParam=DateUtils.parseDateToStr(DateUtils.YYYY,date);
        String ymdParam=DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,date);
        if(yearNow.equals(yearParam)){
            //今年
            if(ymdNow.equals(ymdParam)){
                //今天
                return DateUtils.parseDateToStr(DateUtils.MM_SS,date);
            }
            return DateUtils.parseDateToStr(DateUtils.MM_DD_MM_SS,date);
        }else{
            //往年
            return DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_MM_SS,date);
        }

    }

    /*@PostMapping("/getList")
    public AjaxResult getList(Long bbsId,Long parentId){
        startPage();
        List<BbsComments> commentsList = commentsService.findComment(bbsId,parentId,null);
        return AjaxResult.success(getParent(commentsList));
    }
    private List<BbsComments> getParent(List<BbsComments> rawComments) {
        for (BbsComments topComment : rawComments) {
            LinkedList<BbsComments> comments = new LinkedList<>();
            List<BbsComments> replyByTopCmt = topComment.getReplyComments();
            for (BbsComments replyComment : replyByTopCmt) {
                handleChild(replyComment, comments);
                replyComment.setPublishName(topComment.getPublishName());
            }
            topComment.setReplyComments(comments);
        }
        return rawComments;
    }
    private void handleChild(BbsComments replyComment, List<BbsComments> parent) {
        List<BbsComments> grandchildren = replyComment.getReplyComments();
        replyComment.setReplyComments(null);
        parent.add(replyComment);
        for (BbsComments grandChild : grandchildren) {
            grandChild.setParentName(replyComment.getPublishName());
            if (grandChild.getReplyComments() != null) handleChild(grandChild, parent);
        }
    }
*/
}
